#!/bin/bash

# JKII post flight script to remove old Jedi Knight II and Jedi Knight II MP apps
# 8/9/06 SFM Original version

function error()
{
    echo "*********" "$1" 
    exit
}

# debug
echo "arguments passed to post.sh:"
echo "$1"
echo "$2"
echo "$3"
echo "$4"

# The installer shell should write the location of the game folder
# into /tmp
GameFolder="`cat /tmp/AspyrInstaller.txt`"

echo "GameFolder is: "
echo "$GameFolder"

# Copy the game files from /tmp into the game folder
# cp -r "/tmp/AspyrInstaller/"* "$GameFolder" 
# ditto -V "/tmp/AspyrInstaller" "$GameFolder"

cp "$3"/"tmp/AspyrInstaller/Call of Duty 2 Data/main/iw_15.iwd" "$GameFolder"/"Call of Duty 2 Data/main/"
cp -R "$3"/"tmp/AspyrInstaller/Call of Duty 2.app" "$GameFolder"
cp -R "$3"/"tmp/AspyrInstaller/Call of Duty 2 Multiplayer.app" "$GameFolder"
cp -R "$3"/"tmp/AspyrInstaller/Call of Duty 2 Data/pb" "$GameFolder"/"Call of Duty 2 Data/"

echo "DEBUG: ls'ing the tmp/AspyrInstaller/pb folder"
ls -al "$3"/"tmp/AspyrInstaller/Call of Duty 2 Data/pb"

echo "DEBUG: ls'ing the GameFolder/Call of Duty 2 Data/pb folder"
ls -al "$GameFolder"/"Call of Duty 2 Data/pb"

chmod g+w "$GameFolder"/"Call of Duty 2 Data/pb/"
chmod g+w "$GameFolder"/"Call of Duty 2 Data/pb/"*

if [ -e "$GameFolder"/"Call of Duty 2 Data/main/localized_english_iw00.iwd" ]
     then
          echo "DEBUG: english folder found"
          cp "$3""tmp/AspyrInstaller/Call of Duty 2 Data/main/localized_english_iw11.iwd" "$GameFolder"/"Call of Duty 2 Data/main"
     elif [ -e "$GameFolder"/"Call of Duty 2 Data/main/localized_french_iw00.iwd" ]
     then
          echo "DEBUG: french folder found"
          cp "$3""tmp/AspyrInstaller/Call of Duty 2 Data/main/localized_french_iw11.iwd" "$GameFolder"/"Call of Duty 2 Data/main"
     elif [ -e "$GameFolder"/"Call of Duty 2 Data/main/localized_german_iw00.iwd" ]
     then
          echo "DEBUG: german folder found"
          cp "$3""tmp/AspyrInstaller/Call of Duty 2 Data/main/localized_german_iw11.iwd" "$GameFolder"/"Call of Duty 2 Data/main"
     else
          echo "Call of Duty 2 does not seem to be installed correctly. The localized_<english, french, german>_iw00.iwd files could be found."
fi

# Clean up the installation.
# If system is running 10.3, remove the contents of the tmp/AspyrInstaller
# folders. If the system is running 10.4, remove the AspyrInstaller folder too.

if [ `sw_vers -productVersion | grep -w "10.3"` ]
	then 
	{
		echo "IT'S 10.3, leave /tmp alone, but delete the contents of /tmp/AspyrInstaller/"
		rm -r "$3"/"tmp/AspyrInstaller/Call of Duty 2.app"
		rm -r "$3"/"tmp/AspyrInstaller/Call of Duty 2 Multiplayer.app"
		rm -r "$3"/"tmp/AspyrInstaller/Call of Duty 2 Data"
	}
	else
	{
		echo "IT'S NOT 10.3, clean up the /tmp folder"
		echo "Cleaning up the /tmp folder..."
		rm -r "$3"/"tmp/AspyrInstaller"
		# rm "/tmp/AspyrInstaller.txt"
		echo "Finished cleaning up the /tmp folder."
	}
fi


# Specific to the JKII project, delete the old application bundles.
# rm -rf "Jedi Knight II/" || error "error deleting Jedi Knight II/"
# rm -rf "Jedi Knight II MP/" || error "error deleting Jedi Knight II MP/"

exit 0
